<?php
// 處室資料升級
class Default_Model_Upgrade_ScoreSetup extends Default_Model_Upgrade_Core
{
    protected $_table = 'score_setup';
    protected $_targetTable = array('semester_examine');

    function upgrade (){
        if ($this->isUpgrade())
        return 'isUpgrde';
        else {
            $query = "SELECT * FROM score_setup";
            $res = $this->_sfs3Pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
            $log = array();
            $success_count = 0;
            $error_count = 0;
            $rank = 0;

            foreach($res as $row) {
                $arr = array();
                $arr['year'] = $row['year'];
                $arr['semester'] = $row['semester'];
                $arr['grade'] = $row['class_year'];
                $find = $this->_newdbPdo->query("SELECT year FROM semester_examine
				  WHERE year='{$arr['year']}'
				  AND semester='{$arr['semester']}'
				  AND grade='{$arr['grade']}'
				  ");
                if ($find->rowCount() > 0)
                continue;
                $arr['mode'] = ($row['score_mode'] == 'severally')?2:1;
                $arr['period'] = $row['performance_test_times'];
                $arr['ratio'] = $row['test_ratio'];
                $arr['judgement'] = $this->iconv($row['rule']);
                $query  = "INSERT INTO  semester_examine (".implode(',',array_keys($arr)).") VALUES('".implode("','",array_values($arr))."')";
                $this->_newdbPdo->exec($query);
            }
        }


        //$log['success_count'] = $success_count;
        //$log['error_count'] = $error_count;
        $log['memo'] = '學期成績設定';
        $log['upgrade_time']	= date("Y-m-d h:i:s");

        $this->finishUpgrade($log);

    }

}